<?php

namespace App\Http\Middleware;

use Closure;
use App\Admin;

class AdminMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($req, Closure $next)
    {
        $admins = new Admin();
        $admin = $admins::where('adminname', $req->Cookie('adminname'))->first();
        if ( !($admin && ($admin->permission == 1)) ) {
            return redirect('admin/login');
        }

        return $next($req);
    }
}
